//
//  ChannnelTopScrollView.m
//  ChannelCommonComponent
//
//  Created by LiuLian on 14-2-18.
//  Copyright (c) 2014年 LiuLian. All rights reserved.
//

#import "ChannnelTopScrollView.h"
#import "CLGloble.h"
#import "ChannelComponent.h"

@implementation ChannnelTopScrollView

- (id)initWithDataSource:(ChannelDataSource *)data
{
    _topData = data;
    CGRect frame = CGRectMake(34.0, 20.f, GlobleWidth-26.0, _topData.topScrollViewHeight);
    self.contentInset = UIEdgeInsetsMake(0, _topData.edgeInsetLeft, 0, _topData.edgeInsetRight);
    _channelData = [NSArray arrayWithArray:_topData.channelDic];
    [self initWithChannelListTitleWith:_channelData];
    self = [self initWithFrame:frame];
    if (self) {
        // Initialization code
        [self createItems];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
        self.pagingEnabled = NO;
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        self.scrollsToTop = NO;
        
        
        
//        [[NSNotificationCenter defaultCenter] addObserver:self
//                                                 selector:@selector(updateCSS)
//                                                     name:@"NightModeDidChangeNotification"
//                                                   object:nil];
        
        //初始化数据
        _buttons = [NSMutableArray new];
        _currentButtonTag = 100;
    }
    return self;
}

- (void)initWithChannelListTitleWith:(NSArray *)array
{
    if (!array) {
        return;
    }
    
    for (NSDictionary *data in array) {
        if (!data) {
            return;
        }
        NSString *string = [data objectForKey:@"channelName"];
        if (!string && [string length] < 1) {
            return;
        }
        [_topData.channel addObject:string];
    }
    _channelList = [NSArray arrayWithArray:_topData.channel];
    [_topData.channel removeAllObjects];
}


- (BOOL)touchesShouldCancelInContentView:(UIView *)view{
    if(UMSYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")){
        return YES;
    }else{
        return [super touchesShouldCancelInContentView:view];
    }
    
}

- (void)createItems
{
   
    for (NSInteger i = 0; i < [_channelList count]; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
       // button.userInteractionEnabled=NO;
        
        [button setTag:i+100];
        [button setTitle:[NSString stringWithFormat:@"%@",[_channelList objectAtIndex:i]] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:19.0];
        [button setTitleColor:RGB(0x75, 0x79, 0x84) forState:UIControlStateNormal];
        [button setTitleColor:RGB(0xf9, 0x26, 0x30) forState:UIControlStateSelected];
        [button setTitleEdgeInsets:UIEdgeInsetsMake(-7, -15, 0, 0)];
        [button setFrame:CGRectMake([self getButtonOriginXWithButton:button WithIndex:i], self.frame.size.height-BUTTONHEIGHT+1, [self getButtonWidthXWithButton:button], BUTTONHEIGHT)];
        [button addTarget:self action:@selector(selectChannelButton:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        
        if (i == 0) {
            button.selected = YES;
        }
        
        [_buttons addObject:button];
    }
    
    UIButton *lastButton = [_buttons lastObject];
    [self setContentSize:CGSizeMake(_itemOriginX+lastButton.frame.size.width, self.frame.size.height)];
    [self setContentOffset:CGPointMake(-_topData.edgeInsetLeft, 0)];
}

- (void)updateItemsWithArray:(NSArray *)reload
{
    NSString *currentTitle = [self getChannelWithTag:_currentButtonTag];
    
    _channelData = [NSArray arrayWithArray:reload];
    [self initWithChannelListTitleWith:_channelData];
    
    //清理数据
    for (UIButton *button in self.subviews) {
        [button removeFromSuperview];
    }
    [_buttons removeAllObjects];
    _itemOriginX = 0;
    
    [self createItems];
    
    _currentButtonTag = [self getCurrentTagWithChannel:currentTitle];

    //保证reload数据会重新加载
    [ChannelComponent onlyControllerScrollView].lastCurrentIndex = -2;
    [ChannelComponent onlyControllerScrollView].contentSize = CGSizeMake([_channelList count]*GlobleWidth, [ChannelComponent onlyControllerScrollView].frame.size.height);
    [ChannelComponent updateButtonStateWithTag:_currentButtonTag];
}

- (void)updateItemsWithGoToHomeChannel
{
    //保证reload数据会重新加载
    [ChannelComponent onlyControllerScrollView].lastCurrentIndex = -2;
    [[ChannelComponent onlyControllerScrollView] setContentOffset:CGPointMake(0, [ChannelComponent onlyControllerScrollView].frame.size.height) animated:NO];
    [ChannelComponent onlyControllerScrollView].contentSize = CGSizeMake([_channelList count]*GlobleWidth, [ChannelComponent onlyControllerScrollView].frame.size.height);
    [ChannelComponent updateButtonStateWithTag:100];
}

- (CGFloat)getButtonOriginXWithButton:(UIButton *)_button WithIndex:(NSInteger)_index
{
    UIButton *button = (UIButton *)[self viewWithTag:_index+100-1];
    return _itemOriginX += button.frame.size.width;
}

- (CGFloat)getButtonWidthXWithButton:(UIButton *)_button
{
    return [_button.titleLabel.text length]*19.0+15.0;
}

- (void)selectChannelButton:(UIButton *)sender
{
    [ChannelComponent updateButtonStateWithTag:sender.tag];
    [ChannelComponent updateDataSourceWithInCurrent];
}

//根据位置取频道
- (NSString *)getChannelWithTag:(NSInteger)currentTag
{
    UIButton *currentButton = (UIButton *)[self viewWithTag:currentTag];
    return currentButton.titleLabel.text;
}

//根据频道取位置
- (NSInteger)getCurrentTagWithChannel:(NSString *)title
{
    for (UIButton *button in _buttons) {
        if ([button.titleLabel.text isEqualToString:title]) {
            return button.tag;
        }
    }
    
    UIButton *firstButton = [_buttons objectAtIndex:0];
    return firstButton.tag;
}

//夜间模式
- (void)updateCSS
{
    for(UIView *view in self.subviews){
        if([view isKindOfClass:[UIButton class]]){
            [(UIButton *)view setTitleColor:RGB(0x75, 0x79, 0x84)
                                   forState:UIControlStateNormal];
            [(UIButton *)view setTitleColor:RGB(0xf9, 0x26, 0x30)
                                   forState:UIControlStateSelected];
            
        }
    }
}

@end
